home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 5 / Gekikoh Dennoh Club Vol. 5 (Japan).7z / Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin / internet / xip / iijppp.lzh / src / vjcomp.c < prev   
C/C++ Source or Header  |  1993-09-11  |  3KB  |  144 lines

  1. /*
  2.  *           Input/Output VJ Compressed packets
  3.  *
  4.  *        Written by Toshiharu OHNO (tony-o@iij.ad.jp)
  5.  *
  6.  *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
  7.  *
  8.  * Redistribution and use in source and binary forms are permitted
  9.  * provided that the above copyright notice and this paragraph are
  10.  * duplicated in all such forms and that any documentation,
  11.  * advertising materials, and other materials related to such
  12.  * distribution and use acknowledge that the software was developed
  13.  * by the Internet Initiative Japan, Inc.  The name of the
  14.  * IIJ may not be used to endorse or promote products derived
  15.  * from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  *
  20.  *  TODO:
  21.  */
  22. #include "fsm.h"
  23. #include "lcpproto.h"
  24. #include <netinet/in_systm.h>
  25. #include <netinet/ip.h>
  26. #include "slcompress.h"
  27. #include "hdlc.h"
  28. #include "ipcp.h"
  29.  
  30. #define MAX_VJHEADER 16    /* Maximum size of compressed header */
  31.  
  32. struct slcompress cslc;
  33.  
  34. void
  35. VjInit()
  36. {
  37.   sl_compress_init(&cslc);
  38. }
  39.  
  40. void
  41. SendPppFlame(pri, bp)
  42. int pri;
  43. struct mbuf *bp;
  44. {
  45.   int type;
  46.   int proto;
  47.   int cproto = IpcpInfo.his_compproto >> 16;
  48.  
  49. #ifdef DEBUG
  50.   logprintf("SendPppFlame: proto = %x\n", IpcpInfo.his_compproto);
  51. #endif
  52.   if (cproto== PROTO_VJCOMP) {
  53.     type = sl_compress_tcp(bp, MBUF_CTOP(bp), &cslc, IpcpInfo.his_compproto & 0xff);
  54.  
  55. #ifdef DEBUG
  56.     logprintf("type = %x\n", type);
  57. #endif
  58.     switch (type) {
  59.     case TYPE_IP:
  60.       proto = PROTO_IP;
  61.       break;
  62.     case TYPE_UNCOMPRESSED_TCP:
  63.       proto = PROTO_VJUNCOMP;
  64.       break;
  65.     case TYPE_COMPRESSED_TCP:
  66.       proto = PROTO_VJCOMP;
  67.       break;
  68.     default:
  69.       logprintf("unknown type %x\n", type);
  70.       pfree(bp);
  71.       return;
  72.     }
  73.   } else
  74.     proto = PROTO_IP;
  75.   HdlcOutput(pri, proto, bp);
  76. }
  77.  
  78. static struct mbuf *
  79. VjUncompressTcp(bp, type)
  80. struct mbuf *bp;
  81. u_char type;
  82. {
  83.   u_char *bufp;
  84.   int len, olen, rlen;
  85.   struct mbuf *nbp;
  86.   u_char work[MAX_HDR+MAX_VJHEADER];   /* enough to hold TCP/IP header */
  87.  
  88.   olen = len = plength(bp);
  89.   if (type == TYPE_UNCOMPRESSED_TCP) {
  90.     /*
  91.      * Uncompressed packet does NOT change its size, so that we can
  92.      * use mbuf space for uncompression job.
  93.      */
  94.     bufp = MBUF_CTOP(bp);
  95.     len = sl_uncompress_tcp(&bufp, len, type, &cslc);
  96.     return(bp);
  97.   }
  98.   /*
  99.    *  Handle compressed packet.
  100.    *    1) Read upto MAX_VJHEADER bytes into work space.
  101.    *    2) Try to uncompress it.
  102.    *    3) Compute amount of necesary space.
  103.    *    4) Copy unread data info there.
  104.    */
  105.   if (len > MAX_VJHEADER) len = MAX_VJHEADER;
  106.   rlen = len;
  107.   bufp = work + MAX_HDR;
  108.   bp = mbread(bp, bufp, rlen);
  109.   len = sl_uncompress_tcp(&bufp, olen, type, &cslc);
  110.   len -= olen;
  111.   len += rlen;
  112.   nbp = mballoc(len, MB_VJCOMP);
  113.   bcopy(bufp, MBUF_CTOP(nbp), len);
  114.   nbp->next = bp;
  115.   return(nbp);
  116. }
  117.  
  118. struct mbuf *
  119. VjCompInput(bp, proto)
  120. struct mbuf *bp;
  121. int proto;
  122. {
  123.   u_char type;
  124.  
  125. #ifdef DEBUG
  126.   logprintf("VjCompInput (%02x):\n", proto);
  127.   DumpBp(bp);
  128. #endif
  129.  
  130.   switch (proto) {
  131.   case PROTO_VJCOMP:
  132.     type = TYPE_COMPRESSED_TCP;
  133.     break;
  134.   case PROTO_VJUNCOMP:
  135.     type = TYPE_UNCOMPRESSED_TCP;
  136.     break;
  137.   default:
  138.     logprintf("???\n");
  139.     return(bp);
  140.   }
  141.   bp = VjUncompressTcp(bp, type);
  142.   return(bp);
  143. }
  144.